﻿using Client.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
using Client.Models;

namespace Client.Base
{
    public partial class FrmCode : Form
    {
        public FrmCode()
        {
            InitializeComponent();
        }
        List<BaseCmd.TaxCodingReturn> li;
        /// <summary>
        /// 载入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmCode_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Add(GetCode());
        }
        /// <summary>
        /// 检索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {

        }
        private TreeNode GetInfo(TreeNode tn, int id)
        {
            try
            {
                if (tn == null)
                {
                    tn = new TreeNode();
                    tn.Name = "0";
                    tn.Text = OpenType;
                    tn.Tag = "0";
                }
                CustomModel[] cm = GetCustom(li, id);
                if (cm == null)
                {
                    return null;
                }

                for (int i = 0; i < cm.Length; i++)
                {
                    TreeNode tn2 = new TreeNode();
                    tn2.Name = cm[i].FNumber;
                    tn2.Text = cm[i].FName;
                    tn2.Tag = cm[i].FItemID;
                    GetInfo(tn2, Convert.ToInt32(tn2.Tag));
                    tn.Nodes.Add(tn2);
                }
                return tn;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

        private void GetCode()
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.uom.Token);
                RetModel rm = Share.PostData("51API/Invoice/DownLoadTaxCode", dc, null);
                if (rm.Success)
                {
                    BaseCmd.TaxCodingReturn[] taxm = JsonConvert.DeserializeObject<BaseCmd.TaxCodingReturn[]>(rm.Data.ToString());
                    li = taxm.ToList();
                }
                else
                {
                    MessageBox.Show("获取税收分类编码失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
       
    }
}
